package com.huangfu.learning.javabase.java.collections.list;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;

/**
 * Created with IntelliJ IDEA.
 * Author: huangfu.daohui
 * Date: 2018/7/21
 * Time: 13:46
 * Version: 1.0
 * Description:ArrayList常用操作
 * 线程不安全的，对元素的查询速度快。
 **/
public class ArrayListTest {
    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        list.add("杨幂");
        list.add("热吧");
        list.add("baby");
        list.add("赵丽颖");
        list.add("刘亦菲");
        System.out.println("--------list格式------------------");
        System.out.println(list);
        // 遍历的 三种常用方式
        System.out.println("--------遍历1：fori-------");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        System.out.println("--------遍历2：foreach-------");
        for (String l : list) {
            System.out.println(l);
        }
        System.out.println("---------遍历3：java8--下面两种方式等同------");
        list.forEach(s -> System.out.println(s));
        list.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });
        System.out.println("---------转化成数组---------------------");
        Object[] objects = list.toArray();
        System.out.println(objects[1]);
        System.out.println("---------排序---------------------");
        //排序，直接调用sort方法排序，排序方式是自燃排序，即升序排序
        Collections.sort(list);
        System.out.println(list.toString());
        Collections.reverse(list);//降序
        //对象字段排序
        List<TestA> listA = new ArrayList<TestA>();
        //TestA 即是上面类
        TestA a1 = new TestA();
        a1.setName("张大胖");
        a1.setOrder(1);
        listA.add(a1);
        TestA a5 = new TestA();
        a5.setName("王晓梅");
        a5.setOrder(5);
        listA.add(a5);
        TestA a3 = new TestA();
        a3.setName("刘晓彤");
        a3.setOrder(3);
        listA.add(a3);
        System.out.println("listA 排序前：" + listA.toString());
        Collections.sort(listA, new Comparator<TestA>() {
            @Override
            public int compare(TestA o1, TestA o2) {
                //升序
                return o1.getOrder().compareTo(o2.getOrder());
            }
        });

        System.out.println("listB 升序排序后：" + listA.toString());

    }
}

class TestA implements Serializable {

    private static final long serialVersionUID = -8968350748602548887L;

    private String name;


    //变量类型必须要为int的封装类型，因为只有是封装类型才可以调用Integer的方法
    private Integer order;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getOrder() {
        return order;
    }

    public void setOrder(int order) {
        this.order = order;
    }

    @Override
    public String toString() {
        return "TestA{" +
                "name='" + name + '\'' +
                ", order=" + order +
                '}';
    }
}
